#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int head, e[N], ne[N], idx;

void init() {
	head = -1;
	idx = 0;
}

int main() {
	init();
	int m;
	ci >> m;
	while (m--) {
		char a;
		cin >> a;
		if (a == 'H') {
			int x;
			cin >> x;
			e[idx] = x;
			ne[idx] = ne[k];
			ne[k] = idx++;

		} else if (a == 'D') {
			int k;
			cin >> k;
			if (k == 0) {
				head = head[head];
			} else {
				ne[k] = ne[ne[k]];
			}
		} else {
			int k, x;
			cin >> k >> x;


		}
	}
	return 0;
}